package com.studyprojectbacked.controller;

import com.studyprojectbacked.entity.RestBean;
import com.studyprojectbacked.entity.dto.RegisterAccount;
import com.studyprojectbacked.service.AuthorizeService;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
@RequestMapping("/api/auth")
public class AuthorizeController {
  private static final String EMAIL_REGEXP = "^[a-zA-Z0-9._%=-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$";


  @Autowired
  AuthorizeService authorizeService;

  @PostMapping("/valid-register-email")
  public RestBean<String> validateRegisterEmail(@Pattern(regexp = EMAIL_REGEXP, message = "电子邮箱地址格式不正确")
                                          @RequestParam("email") String email,
                                        HttpSession session) {
    String message = authorizeService.sendCodeToEmail(email, session.getId(), false);
    if (message == null) {
      return RestBean.success("邮件已发送，请注意查看");
    } else {
      return RestBean.failure(400, message);
    }
  }

  @PostMapping("/valid-reset-email")
  public RestBean<String> validateResetEmail(@Pattern(regexp = EMAIL_REGEXP, message = "电子邮箱地址格式不正确")
                                        @RequestParam("email") String email,
                                        HttpSession session) {
    String message = authorizeService.sendCodeToEmail(email, session.getId(), true);
    if (message == null) {
      return RestBean.success("邮件已发送，请注意查看");
    } else {
      return RestBean.failure(400, message);
    }
  }

  @PostMapping("/register")
  public RestBean<String> register(@Valid RegisterAccount ra, HttpSession session) {
    String message = authorizeService.validateAndRegister(ra, session.getId());
    if (message == null) {
      return RestBean.success("注册成功");
    } else {
      return RestBean.failure(400, message);
    }
  }

  /**
   * 1.发送邮件
   * 2.验证验证码是否正确，正确就在Session中存入一个标记
   * 3.用户发起重置密码请求，如果存在标记，就成功重置
   */
  @PostMapping("/start-reset")
  public RestBean<String> startRest(@Pattern(regexp = EMAIL_REGEXP) @RequestParam("email") String email,
                                    @Length(min = 6, max = 6) @RequestParam("code") String code,
                                    HttpSession session) {
    String message = authorizeService.validateCode(email, code, session.getId());

    if (message == null) {
      session.setAttribute("reset-password", email);
      return RestBean.success();
    } else {
      return RestBean.failure(400, message);
    }
  }

  @PostMapping("/do-reset")
  public RestBean<String> doReset(@Length(min = 6, max = 16) @RequestParam("password") String password,
                                  HttpSession session) {
    String email = (String) session.getAttribute("reset-password");

    if (email == null) {
      return RestBean.failure(400, "请先完成邮箱验证");
    } else if (authorizeService.resetPassword(password, email)) {
      session.removeAttribute("reset-password");
      return RestBean.success("密码重置成功");
    } else {
      return RestBean.failure(500, "服务器错误，请联系管理员");
    }
  }
}
